import VueRouter from 'vue-router'
import {
  BaseLayout
} from '../Layouts'

const routes = [{
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    component: BaseLayout,
    path: '/',
    children: [{
        path: '/',
        name: 'Home',
        component: () => import( /* webpackChunkName: "home" */ '../views/Home.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/user/info',
        name: 'UserInfo',
        component: () => import( /* webpackChunkName: "userinfo" */ '../views/UserInfo.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/user/skill',
        name: 'Skill',
        component: () => import( /* webpackChunkName: "skill" */ '../views/UserSkill.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/user/exp',
        name: 'Exp',
        component: () => import( /* webpackChunkName: "exp" */ '../views/UserExp.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/blog',
        name: 'Blog',
        component: () => import( /* webpackChunkName: "blog" */ '../views/Blog.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/type',
        name: 'Type',
        component: () => import( /* webpackChunkName: "type" */ '../views/Type.vue'),
        meta: {
          auth: true
        }
      },
      {
        path: '/blog/create',
        name: 'blogCreate',
        component: () => import( /* webpackChunkName: "blogCreate" */ '../views/BlogCreate.vue'),
        meta: {
          auth: true
        }
      }
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('_token');
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (to.meta.auth) {
      window.antd.message.error('未登录 或 已失效...')
      next('/login')
    } else {
      next()
    }
  }

  next()
})

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

export default router
